﻿using System;
using System.Windows.Forms;

namespace Latin_Vocabulary_Indicator
{
    public partial class setword : Form
    {
        private wordlist _caller;
        private short _editFrom;
        private TextBox[] verbBox = new TextBox[4];
        private Label[] verbLabel = new Label[4];

        public setword(wordlist caller, short editFrom)
        {
            InitializeComponent();
            InitializeArray();
            _caller = caller;
            _editFrom = editFrom;
            if (editFrom != -1)
            {
                byte offered= OfferBox(fileOperator.togglePos(_caller.list.Items[_editFrom].SubItems[2].Text));
                for (byte x = 0; x <= 3; x++)
                {
                    if (offered == x) break;
                    if (_caller.list.Items[_editFrom].SubItems[1].Text.Split(',').Length - 1 < x) verbBox[x].Text = "";
                    else verbBox[x].Text = _caller.list.Items[_editFrom].SubItems[1].Text.Split(',')[x].Trim();
                }
                english.Text = _caller.list.Items[_editFrom].SubItems[3].Text.Trim();
                note.Text = fileOperator.accentTransform( _caller.list.Items[_editFrom].SubItems[4].Text.Trim(),true);
                next.Enabled = false;
            }
            english.Focus();
            english.SelectionStart = english.TextLength;
        }

        private void InitializeArray()
        {
            verbBox[0] = verb_1;
            verbBox[1] = verb_2;
            verbBox[2] = verb_3;
            verbBox[3] = verb_4;
            verbLabel[0] = verb_t1;
            verbLabel[1] = verb_t2;
            verbLabel[2] = verb_t3;
            verbLabel[3] = verb_t4;
        }

        private byte OfferBox(string pos)
        {
            posser.Text = pos;
            Action<byte> offer = (boxOccurs) =>
            {
                for (byte x = 0; x <= 3; x++)
                {
                    verbBox[x].Clear();
                    verbLabel[x].Text="";
                    verbBox[x].Visible = verbLabel[x].Visible = x <= boxOccurs - 1;
                }
            };
            yes.Enabled =  true;
            next.Enabled = _editFrom == -1;
            try
            {
                if (pos.ToLower() == "feminine" || pos.ToLower() == "masculine" || pos.ToLower() == "neuter")
                {
                    offer(2);
                    verbLabel[0].Text = "nominative";
                    verbLabel[1].Text = "genitive";
                    return 2;
                }
                else if (pos.ToLower() == "multigender")
                {
                    offer(4);
                    verbLabel[0].Text = "masculine:\r\nnominative";
                    verbLabel[1].Text = "masculine:\r\ngenitive";
                    verbLabel[2].Text = "feminine:\r\nnominative";
                    verbLabel[3].Text = "feminine:\r\ngenitive";
                    return 4;
                }
                else if (pos.ToLower() == "adjective")
                {
                    offer(3);
                    verbLabel[0].Text = "masculine";
                    verbLabel[1].Text = "feminine";
                    verbLabel[2].Text = "neuter";
                    return 3;
                }
                else if (pos.ToLower() == "verb")
                {
                    offer(4);
                    verbLabel[0].Text = "active indicative present singular first";
                    verbLabel[1].Text = "active present infinitive";
                    verbLabel[2].Text = "active indicative perfect singular first";
                    verbLabel[3].Text = "perfect passive participle";
                    return 4;
                }
                else
                {
                    offer(1);
                    verbLabel[0].Text = "[uninflected]";
                    return 1;
                }
            }
            finally
            {
                verb.Visible = true;
                verbBox[0].Focus();
            }
        }

        private void msg(string input)
        {
            Enabled = false; new message(input, () => { Enabled = true; Focus(); }).Show();
        }
        private void next_Click(object sender, System.EventArgs e)
        {

            bool attempt = cancelAttempt;
            cancelAttempt = false;

            if (verb.Visible == false) { msg("Please use a proper pos.");posser.Focus(); return; }
            if (english.Text.Trim() == "") { msg("\"English meaning\" shouldn't be empty.");english.Focus(); return; }
            if (english.Text.Contains("|") || english.Text.Contains("\\") || note.Text.Contains("|") || note.Text.Contains("\\"))
                { msg("char \"|\" or \"\\\" should not exist in \"English meaning\".");english.Focus(); return; }
            if (note.Text.Contains("`") ){ msg("char \"`\" should not exist in \"Note\"."); note.Focus(); return; }
            for (byte x = 0; x <= 3; x++)
            {
                if (verbBox[x].Visible == false) break;
                bool doubleBreak = false;
                if (verbBox[x].Text.Trim() == "" && posser.Text == "Verb")
                {
                    doubleBreak = true;
                    for (byte y = x; y <= 3; y++)
                    {
                        if(verbBox[x].Visible&& verbBox[x].Text.Trim().Length > 0)
                        {
                            doubleBreak = false;
                            break;
                        }
                    }
                }
                if (doubleBreak) break;
                if (verbBox[x].Text.Trim() == "") { msg("\"" + verbLabel[x].Text + "\" shouldn't be empty."); verbLabel[x].Focus(); return; }
                if (verbBox[x].Text.Contains("|") || verbBox[x].Text.Contains("`") || verbBox[x].Text.Contains(",")) { msg("char \"|\" or \"`\" or \",\" should not exist in \"" + verbLabel[x].Text + "\"."); verbLabel[x].Focus(); return; }
            }
            ListViewItem newitem = new ListViewItem();
            newitem.Text = (_editFrom==-1)?(_caller.list.SelectedIndices.Count == 0 ?( _caller.list.Items.Count).ToString():( _caller.list.SelectedIndices[_caller.list.SelectedIndices.Count - 1]+1).ToString()):(_caller.list.Items[_editFrom].Text);
            newitem.SubItems.Add("nulla");
            newitem.SubItems.Add("nulla");
            newitem.SubItems.Add("nulla");
            newitem.SubItems.Add("nulla");
            string trimstr = "";
            for (byte x = 0; x <= 3; x++)
            {
                if (verbBox[x].Visible == false) break;
                trimstr +=(x+1>3 || verbBox[x+1].Visible == false || verbBox[x + 1].Text.Trim().Length==0) ? verbBox[x].Text.Trim():  verbBox[x].Text.Trim() + ", ";
            }
            newitem.SubItems[1].Text = trimstr;
            newitem.SubItems[2].Text = fileOperator.togglePos(posser.Text);
            newitem.SubItems[3].Text = english.Text.Trim();
            newitem.SubItems[4].Text = fileOperator.accentTransform( note.Text.Trim(),false);
            if(_editFrom==-1)
            foreach (ListViewItem item in _caller.list.Items)
            {
                if (item.SubItems[1].Text == newitem.SubItems[1].Text)
                {
                    msg("Latin meaning already exists.");
                    return;
                }
                else if (item.SubItems[3].Text == newitem.SubItems[3].Text)
                {
                    msg("English meaning already exists.");
                    return;
                }
            }
            if (_editFrom == -1)
            {
                short insertPlace =(short)( _caller.list.SelectedIndices.Count == 0 ? _caller.list.Items.Count : _caller.list.SelectedIndices[_caller.list.SelectedIndices.Count - 1] + 1);
                _caller.list.Items.Insert(insertPlace, newitem);
                for (short x = (short)(insertPlace + 1); x <= _caller.list.Items.Count - 1; x++) _caller.list.Items[x].Text = (Convert.ToInt16(_caller.list.Items[x].Text)+1).ToString();
                _caller.list.SelectedItems.Clear();
                newitem.Selected = true;
            }
            else
            {
                _caller.list.Items[_editFrom] = newitem;
                _caller.list.Items[_editFrom].Selected = true;
            }
            posser_Enter(null, null);
            english.Clear();
            note.Clear();
            english.Focus();
            if (_editFrom != -1) cancel_Click(null, null);
            if (attempt && _editFrom == -1) cancel_Click(null, null);
            _caller.modified = true;
        }

        private void posser_TextChanged(object sender, EventArgs e)
        {
            if (posser.Text.ToLower() == "v") OfferBox("Verb");
            else if (posser.Text.ToLower() == "m") OfferBox("Masculine");
            else if (posser.Text.ToLower() == "f") OfferBox("Feminine");
            else if (posser.Text.ToLower() == "n") OfferBox("Neuter");
            else if (posser.Text.ToLower() == "adj") OfferBox("Adjective");
            else if (posser.Text.ToLower() == "adv") OfferBox("Adverb");
            else if (posser.Text.ToLower() == "pron") OfferBox("Pronoun");
            else if (posser.Text.ToLower() == "prep") OfferBox("Preposition");
            else if (posser.Text.ToLower() == "conj") OfferBox("Conjunction");
            else if (posser.Text.ToLower() == "int") OfferBox("Interjection");
            else if (posser.Text.ToLower() == "two") OfferBox("Multigender");
            else if (posser.Text.ToLower() == "r") OfferBox("Interrogation");
            else if (posser.Text.ToLower() == "\\") OfferBox("\\");
        }

        private void posser_Enter(object sender, EventArgs e)
        {
            if (verb.Visible)
            {
                posser.Clear();
                verb.Visible = false;
                for (byte x = 0; x <= 3; x++)
                {
                    verbBox[x].Clear();
                    verbLabel[x].Text = "";
                }
                yes.Enabled = next.Enabled = false;
            }
        }

        //āōīēū ĀŌĪĒŪ
        private bool accentCharsInvoked(char input)
        {
            return (input == 'ā' || input == 'ō' || input == 'ī' || input == 'ē' || input == 'ū' || input == 'Ā' || input == 'Ō' || input == 'Ī' || input == 'Ē' || input == 'Ū');
        }
        private void verb_1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (verbBox[1].Visible) { verbBox[1].Focus(); verbBox[1].SelectionStart = verbBox[1].TextLength; }
                else { e.Handled = true; note.Focus(); note.SelectionStart = note.TextLength; }
            }
            else if (e.KeyChar == (char)8 && verbBox[0].SelectionStart == 0) { e.Handled = true; posser.Focus(); }
            else if (e.KeyChar == '`' && verbBox[0].SelectionStart != 0)
            {
                e.Handled = true;
                verbBox[0].SelectionStart--;
                verbBox[0].SelectionLength = 1;
                verbBox[0].SelectedText = fileOperator.toggleAccent(Convert.ToChar(verbBox[0].SelectedText)).ToString();
            }
            else if (e.KeyChar == ',')
            {
                if (verbBox[0].SelectionStart == 0 || verbBox[0].Text.Substring(verbBox[0].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void posser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)8 && posser.SelectionStart == 0) { english.Focus(); english.SelectionStart = english.TextLength; }
            else if (e.KeyChar == ' ') e.Handled=true;
        }

        private void english_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (verb.Visible) verbBox[0].Focus(); else posser.Focus();
                e.Handled = true;
            }
        }

        private void verb_2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (verbBox[2].Visible) { e.Handled = true; verbBox[2].Focus(); verbBox[2].SelectionStart = verbBox[2].TextLength; }
                else { e.Handled = true; note.Focus(); note.SelectionStart = note.TextLength; }
            }
            else if (e.KeyChar == (char)8 && verbBox[1].SelectionStart == 0) { e.Handled = true; verbBox[0].Focus(); verbBox[0].SelectionStart = verbBox[0].TextLength; }
            else if (e.KeyChar == '`' && verbBox[1].SelectionStart != 0)
            {
                e.Handled = true;
                verbBox[1].SelectionStart--;
                verbBox[1].SelectionLength = 1;
                verbBox[1].SelectedText = fileOperator.toggleAccent(Convert.ToChar(verbBox[1].SelectedText)).ToString();
            }
            else if (e.KeyChar == ',')
            {
                if (verbBox[1].SelectionStart == 0 || verbBox[1].Text.Substring(verbBox[1].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void verb_3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13 || e.KeyChar == ' ')
            {
                e.Handled = true;
                if (verbBox[3].Visible) { e.Handled = true; verbBox[3].Focus(); verbBox[3].SelectionStart = verbBox[3].TextLength; }
                else { e.Handled = true; note.Focus(); note.SelectionStart = note.TextLength; }
            }
            else if (e.KeyChar == (char)8 && verbBox[2].SelectionStart == 0) { e.Handled = true; verbBox[1].Focus(); verbBox[1].SelectionStart = verbBox[1].TextLength; }
            else if (e.KeyChar == '`' && verbBox[2].SelectionStart != 0)
            {
                e.Handled = true;
                verbBox[2].SelectionStart--;
                verbBox[2].SelectionLength = 1;
                verbBox[2].SelectedText = fileOperator.toggleAccent(Convert.ToChar(verbBox[2].SelectedText)).ToString();
            }
            else if (e.KeyChar == ',')
            {
                if (verbBox[2].SelectionStart == 0 || verbBox[2].Text.Substring(verbBox[2].SelectionStart-1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void verb_4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13 || e.KeyChar == ' ') { e.Handled = true; note.Focus(); note.SelectionStart = note.TextLength; }
            else if (e.KeyChar == (char)8 && verbBox[3].SelectionStart == 0) { e.Handled = true; verbBox[2].Focus(); verbBox[2].SelectionStart = verbBox[2].TextLength; }
            else if (e.KeyChar == '`' && verbBox[3].SelectionStart != 0)
            {
                e.Handled = true;
                verbBox[3].SelectionStart--;
                verbBox[3].SelectionLength = 1;
                verbBox[3].SelectedText = fileOperator.toggleAccent(Convert.ToChar(verbBox[3].SelectedText)).ToString();
            }
            else if (e.KeyChar == ',')
            {
                if (verbBox[3].SelectionStart == 0 || verbBox[3].Text.Substring(verbBox[3].SelectionStart - 1, 1) != " ") e.KeyChar = ' ';
                else e.Handled = true;
            }
        }

        private void note_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) { e.Handled = true; next_Click(null, null); }
            else if (e.KeyChar == (char)8 && note.SelectionStart == 0)
            {
                e.Handled = true;
                for (byte x = 3; x >= 0; x--)
                    if (verbBox[x].Visible)
                    {
                        verbBox[x].Focus(); verbBox[x].SelectionStart = verbBox[x].TextLength;
                        break;
                    }
            }
            else if (e.KeyChar == '`' && note.SelectionStart != 0)
            {
                e.Handled = true;
                note.SelectionStart--;
                note.SelectionLength = 1;
                note.SelectedText = fileOperator.toggleAccent(Convert.ToChar(note.SelectedText)).ToString();
            }
        }

        private void correctCase(byte id)
        {
            string newstr = "";
            bool upperCase = true;
            foreach (char subchr in verbBox[id].Text)
            {
                if (upperCase)
                {
                    upperCase = false;
                    newstr += fileOperator.toUpper(subchr);
                }
                else
                    newstr += fileOperator.toLower(subchr);
            }
            byte sel = (byte)verbBox[id].SelectionStart;
            verbBox[id].Text = newstr;
            verbBox[id].SelectionStart = sel;
        }

        private void verb_1_TextChanged(object sender, EventArgs e)
        {
            correctCase(0);
        }

        private void verb_2_TextChanged(object sender, EventArgs e)
        {
            correctCase(1);
        }

        private void verb_3_TextChanged(object sender, EventArgs e)
        {
            correctCase(2);
        }

        private void verb_4_TextChanged(object sender, EventArgs e)
        {
            correctCase(3);
        }

        bool cancelAttempt = false;

        private void yes_Click(object sender, EventArgs e)
        {
            cancelAttempt = true;
            next_Click(null, null);
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void setword_FormClosing(object sender, FormClosingEventArgs e)
        {
            _caller.Show();
            _caller.Focus();
        }
    }
}
